package com.cycling.app.activity;

import android.os.Bundle;
import android.support.annotation.LayoutRes;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.ViewGroup;

import com.cycling.app.R;

/**
 * Created by LiZhanPing on 2017/5/6.
 */

public abstract class BaseActivity extends AppCompatActivity {

    protected Toolbar mToolbar;
    protected ViewGroup mContainer;

    @LayoutRes
    public abstract int layoutResId();

    public abstract void onActivityCreate(@Nullable Bundle savedInstanceState);

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_base);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        mContainer = (ViewGroup) findViewById(R.id.activity_base_container);
        getLayoutInflater().inflate(layoutResId(), mContainer, true);
        onActivityCreate(savedInstanceState);
    }


    @Override
    public void setTitle(CharSequence title) {
        getSupportActionBar().setTitle(title);
    }

    @Override
    public void setTitle(int titleId) {
        getSupportActionBar().setTitle(titleId);
    }

    public void setSubTitle(int subtitleId) {
        getSupportActionBar().setSubtitle(subtitleId);
    }

    public void setSubTitle(CharSequence subTitle) {
        getSupportActionBar().setSubtitle(subTitle);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return onCompatOptionsItemSelected(item);
    }

    protected boolean onCompatOptionsItemSelected(MenuItem item) {
        return false;
    }
}
